<template>
  <div>
    <!-- 添加用户 -->
    <el-button type="success" @click="dialogTableVisible = true"
      >添加用户</el-button
    >
    <!-- 数据渲染到的表格 -->
    <el-table :data="userList" stripe style="width: 100%">
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="role_name" label="昵称"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch
            @change="changeState(scope.row)"
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="修改">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            @click="showEditUsers(scope.row)"
            circle
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="删除">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="deleUsers(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="分配">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-message"
            circle
            @click="showRoles(scope.row)"
          ></el-button>
        </template>
      </el-table-column>

      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[3, 5, 15, 20]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
      <!-- 添加用户弹窗 -->
      <el-dialog title="添加用户" :visible.sync="dialogTableVisible">
        <el-form :model="addUserDara">
          <el-form-item label="用户名">
            <el-input
              autocomplete="off"
              v-model="addUserDara.username"
            ></el-input>
          </el-form-item>
          <!--         <el-form-item label="密码">
          <el-input
            autocomplete="off"
            v-model="addUserDara.password"
          ></el-input>
        </el-form-item> -->
          <el-form-item label="手机号">
            <el-input
              autocomplete="off"
              v-model="addUserDara.mobile"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input autocomplete="off" v-model="addUserDara.email"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogTableVisible = false">取 消</el-button>
          <el-button type="primary" @click="postUser">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 修改弹窗 -->
      <el-dialog title="修改用户" :visible.sync="dialogTableEditUser">
        <el-form :model="userEditData">
          <el-form-item label="用户名">
            <el-input
              autocomplete="off"
              v-model="userEditData.username"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input
              autocomplete="off"
              v-model="userEditData.mobile"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input
              autocomplete="off"
              v-model="userEditData.email"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogTableEditUser = false">取 消</el-button>
          <el-button type="primary" @click="editPutUser">确 定</el-button>
        </div>
      </el-dialog>
    </el-table>
    <!-- 分配角色 -->
    <el-dialog title="用户角色" :visible.sync="dialogFormRoles">
      <el-form :model="userRolesData">
        <el-form-item label="用户名">
          <el-input
            v-model="userRolesData.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 展示用户角色 -->
        <el-select v-model="roleId" placeholder="请选择">
          <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormRoles = false">取 消</el-button>
        <el-button type="primary" @click="editPutRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0, //多少条数据
      pageSize: 3, //刚进入页面时默认渲染多少条的数据
      userList: [],

      dialogTableVisible: false,
      dialogTableEditUser: false,
      addUserDara: {
        //添加
        username: "", //用户名
        password: "", //密码
        email: "", // 邮箱
        mobile: "", //手机号
      },
      userEditData: {}, // 修改
      //角色分配
      dialogFormRoles: false,
      userRolesData: {},
      roleList: [],
      roleId: "",
    };
  },
  methods: {
    //分配用户角色
    async showRoles(userinfo) {
      this.role_name = userinfo.role_name;
      this.userRolesData = userinfo;

      this.dialogFormRoles = trvue;
      //获取所有角色信息
      var { data } = await this.$req.get("roles");
      // console.log(data);
      this.roleList = data.data;
    },
    //修改角色信息
    async editPutRole() {
      // this.userRolesData.id
      var { data } = await this.$req.put(
        `users/${this.userRolesData.id}/role`,
        { rid: this.roleId }
      );
      this.dialogFormRoles = false;
      if (data.meta.status == 200) {
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        this.getusers();
      } else {
        this.$message.error(data.meta.msg);
      }
    },



    //删除用户
    deleUsers(userinfo) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        var { data } = await this.$req.delete("/users/" + userinfo.id);
        if (data.meta.status == 200) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        } else {
          this.$message.error(data.meta.msg);
        }
      });
    },
    //添加用户
    async postUser() {
      console.log(this.addUserDara);
      var { data } = await this.$req.post("/users", this.addUserDara);
      if (data.meta.status == 201) {
        this.$message({
          showClose: true,
          message: data.meta.msg,
          type: "success",
        });
        this.dialogTableVisible = false;
        this.getusers(1, this.pagesize);
      } else {
        this.$message.error(data.meta.msg);
      }
    },
    //修改用户状态
    async changeState(rows) {
      console.log(rows);
      var { data } = await this.$req.put(
        `/users/${rows.id}/state/${rows.mg_state}`
      );
      console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          showClose: true,
          message: "恭喜你，状态修改成功",
          type: "success",
        });
      } else {
        this.$message.error(data.meta.mag);
      }
    },

    //修改后确定按钮
    async editPutUser() {
      var { data } = await this.$req.put(
        "/users/" + this.userEditData.id,
        this.userEditData
      );
      this.dialogTableEditUser = false;
      if (data.meta.status == 200) {
        this.$message({
          showClose: true,
          message: "恭喜你，修改信息成功",
          type: "success",
        });
      } else {
        this.$message.error(data.meta.mag);
        this.getusers();
      }
      console.log(data);
    },
    // 修改用户信息
    showEditUsers(userinfo) {
      this.userEditData = userinfo;
      this.dialogTableEditUser = true;
    },

    //公共代码  --- 获取用户数据列表
    async getusers(pagenum = 1, pagesize = 3) {
      var { data } = await this.$req.get(
        `/users?pagenum=${pagenum}&pagesize=${pagesize}`
      );
      if (data.meta.status == 200) {
        this.userList = data.data.users;
        this.total = data.data.total;
      } else {
        this.$message.error(data.meta.mag);
      }
    },
    //设置用户列表每一页的条数
    async handleSizeChange(pagesize) {
      this.getusers(1, pagesize);
    },
    //设置跳转到第几页
    handleCurrentChange(pagenum) {
      this.getusers(pagenum, this.pageSize);
    },
  },

  async mounted() {
    this.getusers(1, 3);
  },
};
</script>

<style>
</style>